﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.Utils;
using Newtonsoft.Json;

namespace Ccit.OA.Email
{
    public partial class email_wdyj_view : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {

                ViewState["YJJL"] = (null != Request.QueryString["YJJL"]) ? Request.QueryString["YJJL"].ToString() : "";
                ViewState["SFYD"] = (null != Request.QueryString["SFYD"]) ? Request.QueryString["SFYD"].ToString() : "";
                ViewState["TITLE"] = (null != Request.QueryString["TITLE"]) ? Request.QueryString["TITLE"].ToString() : "";
                ViewState["YJBZ"] = (null != Request.QueryString["YJBZ"]) ? Request.QueryString["YJBZ"].ToString() : "";
                ViewState["YJID"] = (null != Request.QueryString["YJID"]) ? Request.QueryString["YJID"].ToString() : "";
                ViewState["CKLX"] = (null != Request.QueryString["CKLX"]) ? Request.QueryString["CKLX"].ToString() : "";
                ViewState["YJJXH"] = (null != Request.QueryString["YJJXH"]) ? Request.QueryString["YJJXH"].ToString() : "";
                ViewState["Lastspace"] = (null != Request.QueryString["Lastspace"]) ? Request.QueryString["Lastspace"].ToString() : "";
                InitPage();
            }
        }

        private void InitPage()
        {
            string YJJL = ViewState["YJJL"].ToString().Trim();
            string YJID = ViewState["YJID"].ToString().Trim();
            string CKLX = ViewState["CKLX"].ToString().Trim();
            string SFYD = ViewState["SFYD"].ToString().Trim();
            string YJJXH = ViewState["YJJXH"].ToString().Trim();
            string Lastspace = ViewState["Lastspace"].ToString().Trim();
            hidType.Value = ViewState["YJBZ"].ToString().Trim();

            #region 返回上页时的加密参数
            string sparam = "YJJXH=" + YJJXH + "&YJJL=" + ViewState["YJJL"].ToString() + "&TITLE=" + ViewState["TITLE"].ToString() + "&YJBZ=" + ViewState["YJBZ"].ToString();
            string encodeparam = Base64Lib.Encode(sparam);
            hidVar.Value = encodeparam;
            #endregion

            hidEmailId.Value = YJID;
            LoginUser loginUser = Session["LogUserInfo"] as LoginUser;
            hidUserId.Value = loginUser.UserId;
            hiJLBH.Value = YJJL;

            lbFSSJ.Text = DateTime.Now.ToString().Trim();

            EmailBLL bll = new EmailBLL();
            MsEmailDetails msdetails = bll.GetEmailDetails(YJID);
            string EmlType = msdetails.type;
            if (EmlType == "0")
            {
                lbFJR.Text = Ccit.OA.Utils.SysInterface.GetNameStrByIdStr(msdetails.jjuserId, "0", ";");  //收件人
            }
            else {
                lbFJR.Text = msdetails.jjaddress;
            }
            lbFSSJ.Text = msdetails.sendtime;
            lbNBSJR.Text = Ccit.OA.Utils.SysInterface.GetNameStrByIdStr(msdetails.nbsjRange, msdetails.nbsjlogo, ";");
            lbNBCSR.Text = Ccit.OA.Utils.SysInterface.GetNameStrByIdStr(msdetails.nbcsRange, msdetails.nbcslogo, ";");
            lbZT.Text = msdetails.emlTitle;
            lbNR.Text = msdetails.emlContent;

            #region 转发
            sparam = "YJJXH=" + YJJXH + "&YJBZ=C&YJID=" + YJID + "&Lastspace=" + Lastspace;
            encodeparam = Base64Lib.Encode(sparam);
            lnbTurn.Attributes["onclick"] = "javascript:doTurn('" + encodeparam + "');return false;";
            #endregion
            #region 回复
            sparam = "YJJXH=" + YJJXH + "&YJBZ=A&YJID=" + YJID + "&Lastspace=" + Lastspace;
            encodeparam = Base64Lib.Encode(sparam);
            lnbAnswer.Attributes["onclick"] = "javascript:doAnswer('" + encodeparam + "');return false;";
            #endregion
            /*   CKLX  1邮箱查看  0首页查看  */
            if (CKLX == "1")
            {
                HtmlClose.Visible = false;
            }
            else if (CKLX == "0")
            {
                HtmlBack.Visible = false;
            }
            

            #region 显示附件
            AttUpLoad.EditShow("999009", YJID);
            AttUpLoad.ShowStyle = "1";
            #endregion

            /*更新阅读状态*/
            MsEmailRecord msRecord = new MsEmailRecord();
            if (SFYD == "0")
            {
                string sDateTime = DateTime.Now.ToString();
                msRecord.Id = YJJL;
                bll.RecordState(msRecord);

                #region 未读状态时 根据相关标志判断是否自动回复邮件(条件 1、内部邮件  2、阅读时需要回复)
                if (msdetails.type == "0" && msdetails.readlogo == "1")
                {
                    msdetails.emlId = "";
                    msdetails.nbsjlogo = "0";                   //收件人标志
                    msdetails.nbsjRange = msdetails.jjuserId;   //收件人
                    msdetails.sendtime = sDateTime;  //发送时间
                    msdetails.cjdatetime = sDateTime;   //创建时间
                    msdetails.agencId = loginUser.OrganId;//机构ID
                    msdetails.deptId = loginUser.DeptId;
                    msdetails.jobId = loginUser.PosId;
                    msdetails.levelId = loginUser.ClassId;
                    msdetails.cjuserId = loginUser.UserId;  //创建人
                    msdetails.jjuserId = loginUser.UserId;  //发送人
                    msdetails.readlogo = "0";               //读邮件时是否回复  0不回复/1回复
                    msdetails.saveflag = "0";                //是否保存到发件箱子  0不保存/1保存
                    msdetails.emlTitle += " 【回复】";
                    msdetails.emlContent = "    " + loginUser.UserName + "在" + sDateTime + "已阅读此邮件。";
                    msdetails.emlLength = msdetails.emlContent.Length.ToString();
                    bll.SendEmail(msdetails);
                }
                #endregion
            }

            bll.Release();
            bll = null;
            msRecord = null;
            msdetails = null;
            
        }

        /// <summary>
        /// 删除ajax方法 邮件夹下边所有邮件
        /// </summary>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string DeleteEmail(string serExpr)
        {
            bool bFlag = false;
            MsEmailRecord msRecord = (MsEmailRecord)JavaScriptConvert.DeserializeObject(serExpr, typeof(MsEmailRecord));
            EmailBLL bll = new EmailBLL();
            bFlag = bll.DelEmail(msRecord);
            string sResult = (bFlag) ? "1" : "0";
            var newObj = new { resultSign = sResult };
            string resultSerExpr = JavaScriptConvert.SerializeObject(newObj);

            bll.Release();
            bll = null;
            newObj = null;
            msRecord = null;
            return resultSerExpr;
        }
    }
}
